﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;

namespace socketClient
{
    class Program
    {
        public static Socket clientSocket = null;
        public static byte[] data = new byte[10240];

        static void Main(string[] args)
        {
            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
            EndPoint ipEndPoint = new IPEndPoint(ipAddress, 7788);



            //byte[] dataBuffer=new byte[1024];
            //string message = Encoding.UTF8.GetString(dataBuffer, 0, dataBuffer.Length);
            //Console.WriteLine("/"+ message+"/");

            clientSocket.BeginConnect(ipEndPoint, new AsyncCallback(ConnectCallBack), clientSocket);

            //clientSocket.Connect(ipEndPoint);
            while (true)
            {
                //Console.WriteLine("输入数据：");
                string s = Console.ReadLine();
                //当输入c时，Socket关闭
                if (s == "c")
                {
                    clientSocket.Close();
                    return;
                }
                BeginSend(Encoding.UTF8.GetBytes(s));
            }

            Console.ReadKey();
        }

        private static void ConnectCallBack(IAsyncResult result)
        {
            try
            {
                clientSocket = (Socket)result.AsyncState;
                clientSocket.EndConnect(result);
                Console.WriteLine("连接成功");
                Console.WriteLine("等待接受服务器数据");
                BeginReceive(clientSocket);
            }
            catch (SocketException e)
            {
                Console.WriteLine(e);
            }
        }

        private static void BeginReceive(Socket socket)
        {
            socket.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), socket);
        }

        private static void ReceiveCallBack(IAsyncResult result)
        {
            try
            {
                //将传入的clientSocket进行接收，并且完成接收数据的操作
                Socket socket = result.AsyncState as Socket;
                int length = clientSocket.EndReceive(result);
                string message = Encoding.UTF8.GetString(data, 0, length);
                Console.WriteLine("收到消息：" + message);
                BeginReceive(socket);
            }
            catch (SocketException e)
            {
                Console.WriteLine(e);
                if (clientSocket != null)
                {
                    clientSocket.Close();
                    clientSocket = null;
                }
            }
        }

        private static void BeginSend(byte[] sendData)
        {
            clientSocket.BeginSend(sendData, 0, sendData.Length, SocketFlags.None, new AsyncCallback(SendCallBack), clientSocket);
        }

        private static void SendCallBack(IAsyncResult result)
        {
            try
            {
                Console.WriteLine("客户端发送结束");
                Socket clientSocket = (Socket)result.AsyncState;
                clientSocket.EndSend(result);
            }
            catch (SocketException e)
            {
                Console.WriteLine(e);
            }
        }
    }
}
